package com.example.viewpager2demo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class PageFragment : Fragment() {
    private var pageNumber: Int = 0
    private lateinit var innerViewPager: ViewPager2
    private lateinit var innerTabLayout: TabLayout
    private var tabLayoutMediator: TabLayoutMediator? = null
    private lateinit var innerAdapter: InnerPagerAdapter

    companion object {
        private const val ARG_PAGE_NUMBER = "page_number"
        private const val TAG = "PageFragment"

        fun newInstance(pageNumber: Int): PageFragment {
            return PageFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PAGE_NUMBER, pageNumber)
                }
            }
        }

        private fun logFragmentCount(fragmentManager: androidx.fragment.app.FragmentManager, tag: String) {
            val fragments = fragmentManager.fragments
            Log.d(TAG, "$tag - FragmentManager中的Fragment数量: ${fragments.size}")
            fragments.forEachIndexed { index, fragment ->
                Log.d(TAG, "$tag - Fragment $index: ${fragment.javaClass.simpleName}")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            pageNumber = it.getInt(ARG_PAGE_NUMBER)
        }
        Log.d(TAG, "Fragment $pageNumber onCreate")
        logFragmentCount(requireActivity().supportFragmentManager, "onCreate")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d(TAG, "Fragment $pageNumber onCreateView")
        val view = inflater.inflate(R.layout.fragment_page, container, false)
        
        // 设置页面标题
        view.findViewById<TextView>(R.id.tvPageNumber).text = "页面 ${pageNumber + 1}"
        
        // 初始化内部ViewPager2和TabLayout
        innerViewPager = view.findViewById(R.id.innerViewPager)
        innerTabLayout = view.findViewById(R.id.innerTabLayout)
        
        // 设置ViewPager2的适配器
        innerAdapter = InnerPagerAdapter(requireActivity())
        innerViewPager.adapter = innerAdapter
        
        // 设置TabLayout和ViewPager2的联动
        tabLayoutMediator = TabLayoutMediator(innerTabLayout, innerViewPager) { tab, position ->
            tab.text = when (position) {
                0 -> "我的自选"
                1 -> "多股同列"
                2 -> "主力资金"
                else -> "Tab ${position + 1}"
            }
        }
        tabLayoutMediator?.attach()
        
        logFragmentCount(requireActivity().supportFragmentManager, "onCreateView")
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        logFragmentCount(requireActivity().supportFragmentManager, "onViewCreated")
    }

    override fun onResume() {
        super.onResume()
        logFragmentCount(requireActivity().supportFragmentManager, "onResume")
    }

    override fun onPause() {
        super.onPause()
        logFragmentCount(requireActivity().supportFragmentManager, "onPause")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        // 解除TabLayoutMediator的绑定
        tabLayoutMediator?.detach()
        Log.d(TAG, "Fragment $pageNumber onDestroyView")
        logFragmentCount(requireActivity().supportFragmentManager, "onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "Fragment $pageNumber onDestroy")
        logFragmentCount(requireActivity().supportFragmentManager, "onDestroy")
    }

    // 更新页面数据的方法
    fun updatePageData(newData: Any) {
        // 获取当前显示的Fragment
        val currentFragment = when (innerViewPager.currentItem) {
            0 -> childFragmentManager.findFragmentByTag("f0") as? MyStocksFragment
            1 -> childFragmentManager.findFragmentByTag("f1") as? MultiStocksFragment
            2 -> childFragmentManager.findFragmentByTag("f2") as? MainForceFragment
            else -> null
        }
        currentFragment?.updateData(newData)
    }
} 